import axios from 'axios'
import System from './System';
import CookieUtil from './CookieUtil';
import StrUtil from './StrUtil';

export default class HttpUtil {

    static httpAuthUrl:string = <string> import.meta.env.VITE_APP_BASE_API_AUTH

    static httpDataUrl:string = <string> import.meta.env.VITE_APP_BASE_API_DATA

    static init(){
        System.$httpAuth = new AxiosBean(this.httpAuthUrl)
        System.$httpData = new AxiosBean(this.httpDataUrl)
    }

}

class AxiosBean {

    http:any

    constructor(baseUrl:string){

        this.http = axios.create({
            baseURL : baseUrl,
            timeout : 3600000,
        })
                
        this.http.interceptors.request.use(
            async (config:any) => {

                config.headers['Accept'] = '*/*'

                if(System.$store.system.tokenRefresh){

                    if(config.url == 'auth/login/refreshToken'){
                        config.headers['Authorization'] = System.$store.system.token
                    }
                    else{
                        await new Promise( (resolve) =>{
                        　　let timer = setInterval(() =>{
                            　　if(!System.$store.system.tokenRefresh){
                                　　clearInterval(timer)
                                　　resolve(true)
                            　　}
                        　　},100)
                        })
                        config.headers['Authorization'] = System.$store.system.token
                    }
                    return config

                    // return config;
                }

                if(config.url == 'auth/login/password'){
                    config.headers['Authorization'] = System.$store.system.token
                }else{
                    await CookieUtil.getToken().then(res=>{
                        if(StrUtil.isNull(res)){
                            System.$router.push("/login")
                            return
                        }
                        config.headers['Authorization'] = res
                    })
                }
                return config;
            },
            (error:any) => {
                return Promise.reject(error);
            }
        );

        this.http.interceptors.response.use(
            (response:any) => {

                let responseT:any = response.data

                if(responseT.code != '200'){
                    // er(response.msg)
                    return Promise.reject(responseT)
                }

                return Promise.resolve(responseT)
            },
            (error:any) => {
                // er("接口异常")
                return Promise.reject(error)
            }
        );

    }

    get(request:any, data:any = {}){

        // console.log("开始请求",request);
        return new Promise((resolve, reject) => {
            
            let method = request.method,
                params = ['get', 'delete', 'head'].indexOf(method) > -1 ? {
                    params: data
                } : {
                    data: data
                };
            this.http({
                ...request,
                ...params,
            }).then((response:any) => {
                resolve(response);
            }, (err:any) => {
                reject(err)
            })
        })
    }


}